unit templateutils;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fptemplate;
Type
  { TBaseTemplate }

  TBaseTemplate = class
  protected
    fTemplate:TFpTemplate;
  public
    function getTemplate:TFpTemplate;

    constructor create(AFileName:String = '');virtual;
    destructor destroy;override;
  end;

  TTemplateClass = Class of TBaseTemplate;
  THomeTemplate = class;
  { TMainTemplate }

  TMainTemplate = class(TBaseTemplate)
  private
    fPageTitle: string;
    fTitle: string;
    fContentTemplate:TBaseTemplate;
    function getContentTemplate: TBaseTemplate;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);
  public
    property PageTitle :string read fPageTitle write fPageTitle;
    Property Title : string read fTitle write fTitle;
    property ContentTemplate:TBaseTemplate read getContentTemplate write fContentTemplate;

    constructor create(AFileName:String='');override;
    destructor destroy;override;
  end;

  { THomeTemplate }

  THomeTemplate = class(TBaseTemplate)
  private
    fAddressLink,fPersonLink:String;
    function getAddressLink: string;
    function getPersonLink: string;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);
  public
    property PersonLink:string read getPersonLink;
    property AddressLink:string read getAddressLink;

    constructor create;
  end;

  { TPersonTemplate }

  TPersonTemplate = class(TBaseTemplate)
  private
    FColumName: TStringList;
    fPrevLink: string;
    fRecCount: Integer;
    FValues: TStringList;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);virtual;
  public
    property ColumnName :TStringList read FColumName;
    property Values : TStringList read FValues;
    property RecCount : Integer read fRecCount write fRecCount;
    property PrevLink :string read fPrevLink write fPrevLink;

    constructor create(AFileName:String='');override;
    destructor destroy;override;
  end;

  { TOfficeDetailsTemplate }

  TOfficeDetailsTemplate = Class(TBaseTemplate)
  private
    fEmail: string;
    fOfficeAddress: string;
    fOfficeName: string;
    fOID: string;
    fPhone: string;
    fPrevLink: string;
    fWeb: string;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);virtual;
  public
    property OID:string read fOID write fOID;
    property OfficeName: string read fOfficeName write fOfficeName;
    property OfficeAddress: string read fOfficeAddress write fOfficeAddress;
    property Phone: string read fPhone write fPhone;
    property Web: string read fWeb write fWeb;
    property Email: string read fEmail write fEmail;
    property PrevLink :string read fPrevLink write fPrevLink;

    constructor create(AFileName:String='');override;
  end;

  { TContacDetailsTemplate }

  TContactDetailsTemplate = class(TPersonTemplate)
  private
    fPerson: string;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);override;
  public
     property person:string read fPerson write fPerson;

     constructor create(AFileName:String='');override;
  end;

  { TErrorTemplate }

  TErrorTemplate =class(TBaseTemplate)
  private
    fHeading: String;
    fMessage: string;
  protected
    Procedure doGetParams(Sender : TObject;
      Const ParamName : String;
      Out AValue : String);
  public
    property Heading:String read fHeading write fHeading;
    property Message:string read fMessage write fMessage;

    constructor create(AFileName:String);override;
  end;

implementation
uses
  config,
  fpfcgi;

{ TContacDetailsTemplate }

procedure TContactDetailsTemplate.doGetParams(Sender: TObject;
  const ParamName: String; out AValue: String);
begin
  inherited doGetParams(sender,ParamName,AValue);
  case paramName of
  'PERSON':AValue:= Person;
  end;
end;

constructor TContactDetailsTemplate.create(AFileName: String);
begin
  inherited create('');
  fTemplate.FileName:='template/contactdetails.html';
  fTemplate.OnGetParam:=@doGetParams;
end;

{ TErrorTemplate }

procedure TErrorTemplate.doGetParams(Sender: TObject; const ParamName: String;
  out AValue: String);
begin
  case paramName of
  'HEADING':AValue:=Heading;
  'MESSAGE':Avalue:=Message;
  end;
end;

constructor TErrorTemplate.create(AFileName: String);
begin
  inherited create(AFileName);
  fTemplate.OnGetParam:=@doGetParams;
end;

{ TOfficeDetailsTemplate }

procedure TOfficeDetailsTemplate.doGetParams(Sender: TObject;
  const ParamName: String; out AValue: String);
begin
  case ParamName of
  'OID' : AValue:=OID;
  'OFFICENAME' : AValue:=OfficeName;
  'OFFICEADDRESS' : AValue:=OfficeAddress;
  'WEB' : AValue:=Web;
  'EMAIL' : AValue:=Email;
  'PHONE' : AValue:=Phone;
  'PREVLINK' : AValue:=PrevLink;
  end;
end;

constructor TOfficeDetailsTemplate.create(AFileName:string);
begin
  inherited create('template/officedetails.html');
  fTemplate.OnGetParam:=@doGetParams;
end;

{ THomeTemplate }

function THomeTemplate.getAddressLink: string;
begin
  fAddressLink:=TMyURLHelper.SiteUrl('address','index');
  Result:=fAddressLink;
end;

function THomeTemplate.getPersonLink: string;
begin
  fPersonLink:=TMyURLHelper.SiteUrl('person','index');
  Result:=fPersonLink;
end;

procedure THomeTemplate.doGetParams(Sender: TObject; const ParamName: String;
  out AValue: String);
begin
  Case ParamName of
  'PERSONLINK': AValue:=PersonLink;
  'ADDRESSLINK': AValue:=AddressLink;
  end;
end;

constructor THomeTemplate.create;
begin
  inherited create('template/home.html');
  fTemplate.OnGetParam:=@doGetParams;
end;

{ TPersonTemplate }

procedure TPersonTemplate.doGetParams(Sender: TObject; const ParamName: String;
  out AValue: String);
begin
  case ParamName of
  'COLNAME':AValue:=ColumnName.Text;
  'COLVALUE':AValue:=VALUES.text;
  'PREVIOUS':AValue:=fPrevLink;
  'RECCOUNT':AValue:=IntToStr(fRecCount);
  end;
end;

constructor TPersonTemplate.create(AFileName: String);
begin
  inherited create('template/person.html');
  fTemplate.OnGetParam:=@doGetParams;
  fColumName:=TStringList.Create;
  fValues:=TStringList.Create;
end;

destructor TPersonTemplate.destroy;
begin
  FreeAndNil(self.FValues);
  FreeAndNil(self.fColumName);
  inherited destroy;
end;

{ TBaseTemplate }

function TBaseTemplate.getTemplate: TFpTemplate;
begin
  Result:=fTemplate;
end;

constructor TBaseTemplate.create(AFileName: String);
begin
  fTemplate:=TFpTemplate.Create;
  fTemplate.FileName:=AFileName;
  ftemplate.StartDelimiter:='{+';
  fTemplate.EndDelimiter:='+}';
end;

destructor TBaseTemplate.destroy;
begin
  FreeAndNil(fTemplate);
  inherited destroy;
end;

{ TMainTemplate }

function TMainTemplate.getContentTemplate: TBaseTemplate;
begin
  Result:=fContentTemplate;
end;

procedure TMainTemplate.doGetParams(Sender: TObject; const ParamName: String;
  out AValue: String);
begin
  Case ParamName of
  'PAGETITLE': AValue:=fPageTitle;
  'TITLE': AValue:=fTitle;
  'CONTENT':AValue:=ContentTemplate.getTemplate.GetContent;
  end;
end;

constructor TMainTemplate.create(AFileName: String);
begin
  inherited create('template/template.html');
  fTemplate.OnGetParam:=@doGetParams;
end;

destructor TMainTemplate.destroy;
begin
  freeAndNil(fContentTemplate);
  inherited destroy;
end;

end.

